thumbstick2 2.2.0
Loading...
Searching...
No Matches
thumbstick2


Thumbstick 2 Click

Thumbstick 2 Click demo application is developed using the NECTO Studio, ensuring compatibility with mikroSDK's open-source libraries and tools. Designed for plug-and-play implementation and testing, the demo is fully compatible with all development, starter, and mikromedia boards featuring a mikroBUS™ socket.


Click Library

  • Author : Stefan Filipovic
  • Date : Jun 2025.
  • Type : SPI type

Software Support

Example Description

This example demonstrates the use of the Thumbstick 2 Click board which features a 2-axis joystick with push button and vibration feedback. The joystick's angle and position are calculated based on raw ADC values, while the push button status controls the vibration motor through PWM output.

Example Libraries

  • MikroSDK.Board
  • MikroSDK.Log
  • Click.Thumbstick2

Example Key Functions

  • thumbstick2_cfg_setup This function initializes Click configuration structure to initial values.
    void thumbstick2_cfg_setup(thumbstick2_cfg_t *cfg)
    Thumbstick 2 configuration object setup function.
    Thumbstick 2 Click configuration object.
    Definition thumbstick2.h:180
  • thumbstick2_init This function initializes all necessary pins and peripherals used for this Click board.
    err_t thumbstick2_init(thumbstick2_t *ctx, thumbstick2_cfg_t *cfg)
    Thumbstick 2 initialization function.
    Thumbstick 2 Click context object.
    Definition thumbstick2.h:159
  • thumbstick2_read_raw_adc This function reads the raw ADC for X and Y axis by using SPI serial interface.
    err_t thumbstick2_read_raw_adc ( thumbstick2_t *ctx, uint16_t *raw_x, uint16_t *raw_y );
    err_t thumbstick2_read_raw_adc(thumbstick2_t *ctx, uint16_t *raw_x, uint16_t *raw_y)
    Thumbstick 2 read raw adc function.
  • thumbstick2_get_angle This function calculates and returns joystick angle in degrees from raw ADC values for X and Y axis.
    float thumbstick2_get_angle ( uint16_t raw_x, uint16_t raw_y );
    float thumbstick2_get_angle(uint16_t raw_x, uint16_t raw_y)
    Thumbstick 2 get angle function.
  • thumbstick2_get_position This function calculates and returns joystick position flag from raw ADC values for X and Y axis.
    uint8_t thumbstick2_get_position ( uint16_t raw_x, uint16_t raw_y );
    uint8_t thumbstick2_get_position(uint16_t raw_x, uint16_t raw_y)
    Thumbstick 2 get position function.

Application Init

Initializes the logger and the Click driver.

void application_init ( void )
{
log_cfg_t log_cfg;
thumbstick2_cfg_t thumbstick2_cfg;
LOG_MAP_USB_UART( log_cfg );
log_init( &logger, &log_cfg );
log_info( &logger, " Application Init " );
// Click initialization.
thumbstick2_cfg_setup( &thumbstick2_cfg );
THUMBSTICK2_MAP_MIKROBUS( thumbstick2_cfg, MIKROBUS_1 );
if ( THUMBSTICK2_OK != thumbstick2_init( &thumbstick2, &thumbstick2_cfg ) )
{
log_error( &logger, " Communication init." );
for ( ; ; );
}
log_info( &logger, " Application Task " );
}
#define THUMBSTICK2_MAP_MIKROBUS(cfg, mikrobus)
MikroBUS pin mapping.
Definition thumbstick2.h:143
void application_init(void)
Definition main.c:32
@ THUMBSTICK2_OK
Definition thumbstick2.h:206

Application Task

Continuously reads the raw ADC values from the joystick axes, calculates the joystick's position and angle, and logs the results. It also checks the state of the joystick push button and activates vibration feedback accordingly.

void application_task ( void )
{
float angle = 0;
uint16_t raw_x = 0;
uint16_t raw_y = 0;
uint8_t position = 0;
if ( THUMBSTICK2_OK == thumbstick2_read_raw_adc ( &thumbstick2, &raw_x, &raw_y ) )
{
angle = thumbstick2_get_angle ( raw_x, raw_y );
position = thumbstick2_get_position ( raw_x, raw_y );
log_printf ( &logger, " RAW X: %u\r\n RAW Y: %u\r\n", raw_x, raw_y );
log_printf ( &logger, " Joystick angle: %.1f degrees\r\n", angle );
log_printf ( &logger, " Joystick position: " );
switch ( position )
{
{
log_printf ( &logger, "NEUTRAL" );
break;
}
{
log_printf ( &logger, "UP" );
break;
}
{
log_printf ( &logger, "UPPER-LEFT" );
break;
}
{
log_printf ( &logger, "LEFT" );
break;
}
{
log_printf ( &logger, "LOWER-LEFT" );
break;
}
{
log_printf ( &logger, "DOWN" );
break;
}
{
log_printf ( &logger, "LOWER-RIGHT" );
break;
}
{
log_printf ( &logger, "RIGHT" );
break;
}
{
log_printf ( &logger, "UPPER-RIGHT" );
break;
}
default:
{
log_printf ( &logger, "UNKNOWN" );
break;
}
}
log_printf ( &logger, "\r\n" );
if ( thumbstick2_get_sw_pin ( &thumbstick2 ) )
{
log_printf ( &logger, " Button: Idle\r\n" );
log_printf ( &logger, " Vibro: Idle\r\n\n" );
}
else
{
log_printf ( &logger, " Button: Active\r\n" );
log_printf ( &logger, " Vibro: Active\r\n\n" );
}
Delay_ms ( 100 );
}
}
#define THUMBSTICK2_POSITION_DOWN
Definition thumbstick2.h:103
#define THUMBSTICK2_PWM_MIN_DUTY
Definition thumbstick2.h:113
#define THUMBSTICK2_POSITION_UPPER_RIGHT
Definition thumbstick2.h:106
#define THUMBSTICK2_POSITION_NEUTRAL
Thumbstick 2 position values.
Definition thumbstick2.h:98
#define THUMBSTICK2_POSITION_LOWER_RIGHT
Definition thumbstick2.h:104
#define THUMBSTICK2_POSITION_LEFT
Definition thumbstick2.h:101
#define THUMBSTICK2_POSITION_UP
Definition thumbstick2.h:99
#define THUMBSTICK2_PWM_MAX_DUTY
Definition thumbstick2.h:114
#define THUMBSTICK2_POSITION_LOWER_LEFT
Definition thumbstick2.h:102
#define THUMBSTICK2_POSITION_UPPER_LEFT
Definition thumbstick2.h:100
#define THUMBSTICK2_POSITION_RIGHT
Definition thumbstick2.h:105
uint8_t thumbstick2_get_sw_pin(thumbstick2_t *ctx)
Thumbstick 2 get SW pin function.
err_t thumbstick2_set_duty_cycle(thumbstick2_t *ctx, float duty_cycle)
Thumbstick 2 set duty cycle function.
void application_task(void)
Definition main.c:62

Application Output

This Click board can be interfaced and monitored in two ways:

  • Application Output - Use the "Application Output" window in Debug mode for real-time data monitoring. Set it up properly by following this tutorial.
  • UART Terminal - Monitor data via the UART Terminal using a USB to UART converter. For detailed instructions, check out this tutorial.

Additional Notes and Information

The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.